package com.eventur;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ImageView.ScaleType;

public class DisplayActivity extends Activity{
	String title = null;
	String time = null;
	String venue= null;
	String description = null;
	private double currentLatitude = 0.0;
	private double currentLongitude = 0.0;

	private static final int MENU_HOME = Menu.FIRST;
	//private static final int MENU_QUIT = Menu.FIRST+1;

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
	}

	@Override
	protected void onResume(){
		super.onResume();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);

		// Add two example menu buttons
		menu.add(0,MENU_HOME,0,"Home");
		//menu.add(0,MENU_QUIT,1,"Quit");
		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item){
		switch(item.getItemId()	){
		case MENU_HOME: Intent intent = new Intent(this, MainActivity.class);;
		startActivity(intent);finish();
		return true;
		//case MENU_QUIT: int pid = android.os.Process.myPid();
		//android.os.Process.killProcess(pid); finish();
		//return true;

		}
		return false;

	}



	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.detailed);
		TextView titleText = (TextView) findViewById(R.id.titleText);
		TextView timeText = (TextView) findViewById(R.id.timeText);
		TextView venueText = (TextView) findViewById(R.id.venueText);
		TextView descriptionText = (TextView) findViewById(R.id.descriptionText);
		ImageView imageView = (ImageView) findViewById(R.id.imageButton);
		Button mapButton = (Button) findViewById(R.id.mapButton);
		Button browseButton = (Button) findViewById(R.id.browseButton);

		Bundle bundle = getIntent().getExtras();
		String eventTitle = null;
		if (bundle != null) {
			eventTitle = bundle.getString("eventTitle");
			currentLatitude = bundle.getDouble("currentLatitude");
			currentLongitude = bundle.getDouble("currentLongitude");
			
		}
		Event event = new Event();
		ListEvents listEvent = new ListEvents();
		event = listEvent.getEvent(eventTitle);

		titleText.setText(event.getTitle());
		timeText.setText(event.getsDate());
		venueText.setText(event.getVenue());
		final String linkToEvent = event.getLink();
		descriptionText.setText("Yet to find");
		//final String venueAddress = event.getVenueAddress();
		final double latitude = event.getLatitude();
		final double longitude = event.getLongitude();
		browseButton.setOnClickListener( new View.OnClickListener()
		{
			public void onClick(View v)
			{
				setContentView(R.layout.webview);
				WebView wv = (WebView)findViewById( R.id.webviewscreen );
				wv.loadUrl(linkToEvent);
			}
		});
		/*imageView.setOnClickListener( new View.OnClickListener()
		{
			public void onClick(View v)
			{
				setContentView(R.layout.webview);
				WebView wv = (WebView)findViewById( R.id.webviewscreen );
				wv.loadUrl(linkToEvent);
			}
		});*/
		mapButton.setOnClickListener( new View.OnClickListener()
		{
			public void onClick(View v)
			{
				Intent intent = new Intent(DisplayActivity.this, MapViewActivity.class);
				Bundle bundle = new Bundle();
				bundle.putString("key", "single");
				bundle.putDouble("latitude",latitude);
				bundle.putDouble("longitude",longitude);
				System.out.println("placing the current lat lon in bundle dispact:"+currentLatitude+currentLongitude);

				bundle.putDouble("currentLatitude",currentLatitude);
				bundle.putDouble("currentLongitude",currentLongitude);
				intent.putExtras(bundle);
				startActivity(intent);
			}
		});

		Bitmap bitmap;
		try {
			bitmap = BitmapFactory.decodeStream((InputStream)new URL("http://eventur.sis.pitt.edu/images/heinz7.jpg").getContent());
		} catch (MalformedURLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			bitmap = BitmapFactory.decodeFile("assets/heinz7.jpg");
			e1.printStackTrace();
		}
		try {
			try{
				bitmap = BitmapFactory.decodeStream((InputStream)new URL(event.getImageLink()).getContent());}
			catch(Exception e){
				bitmap = BitmapFactory.decodeStream((InputStream)new URL("http://eventur.sis.pitt.edu/images/heinz7.jpg").getContent());
			}

			int width = 0;
			int height =0;
			int newWidth = 85;
			int newHeight = 85;

			try{
				width = bitmap.getWidth();
				height = bitmap.getHeight();
			}
			catch(Exception e){
				width = 65;
				height = 65;
			}
			float scaleWidth = ((float)newWidth)/width;
			float scaleHeight = ((float)newHeight)/height;
			Matrix mat = new Matrix();
			mat.postScale(scaleWidth, scaleHeight);

			try{
				Bitmap newBitmap = Bitmap.createBitmap(bitmap,0,0,width,height,mat,true);
				BitmapDrawable bmd = new BitmapDrawable(newBitmap);
				imageView.setImageDrawable(bmd);
				imageView.setScaleType(ScaleType.CENTER);
			}
			catch(Exception e){

			}

		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}




	}

}
